<?php
	class MarcaDAO{
		protected $db;

		public function __construct($db){
			$this->db = $db;
		}

		public function setDb($db){
			$this->db = $db;
		}

		public function getDb(){
			return $this->db;
		}

		public function addMarca($m){
			$sql = "insert into marca (descr, ativo)
					values (".($m->getDescr() ? "'{$m->getDescr()}'" : "null").",".($m->getAtivo() ? "'{$m->getAtivo()}'" : "null").")";
			$res = $this->getDb()->query($sql);			
			return $res;
		}

		public function setMarca($m){
			$sql = "update marca set descr = ".($m->getDescr() ? "'{$m->getDescr()}'" : "null").", ativo = ".($m->getAtivo() ? "'{$m->getAtivo()}'" : "null")."
					where marca = {$m->getId()}";

			$res = $this->getDb()->query($sql);
			return $res;
		}

		public function getMarca($id){
			$sql = "select MARCA, DESCR, ATIVO
					from marca 
					where marca = {$id}";
			$res = $this->getDb()->query($sql);
			$r = $res[0];

			$m = new Marca($r['MARCA'],$r['DESCR'],$r['ATIVO']);
			return $m;
		}

		public function getLista($where=array()){
			$lista = array();
			$where = ($where ? "where ".(implode(" and ",$where)) : "");

			$sql = "select MARCA, DESCR, ATIVO
					from marca 
					{$where}
					order by DESCR";
			$res = $this->getDb()->query($sql);

			if($res){
				foreach($res as $r){
					$lista[] = new Marca($r['MARCA'],$r['DESCR'],$r['ATIVO']);
				}
			}

			return $lista;
		}
	}
?>